package gctest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description:Finalize()方法学习
 * @author: Lidong
 * @time: 2020/5/20 22:41
 **/
public class GcTest1 {

    public static void main(String[] args) {
        Dog dog1 = new Dog(1);
        Dog dog2 = new Dog(2);
        Dog dog3 = new Dog(3);
        Dog dog4 = new Dog(4);
        dog3=dog4=null;
        System.gc();
    }
}
class Dog extends Object {

    private static final Logger logger = LoggerFactory.getLogger(Dog.class);


    public int id;
    public Dog(int i) {
        this.id = i;
        logger.info("第"+i+"个对象创建完成！");
    }

    /*重写Object的finalize()*/
    @Override
    protected void finalize() throws java.lang.Throwable {
        super.finalize();
        logger.info("第" + id + "个对象被收回");
    }

}
